<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据请求</title>
    <script src="../js/vue.js"></script>
    <script src="../js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #app {
            position: relative;
            overflow: hidden;
        }

        .table {
            width: 500px;
            margin: 50px auto;
            /* height: auto; */
            overflow: hidden;
            position: relative;
        }

        .table .item {
            width: 100px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: burlywood;
            color: cornflowerblue;
            float: left;
        }

        .table .item input {
            width: 100px;
        }
    </style>
</head>

<body>
    <div id="app">

        <div>用户名:<input v-model="username"></div>
        <div>密&nbsp;&nbsp;&nbsp;码:<input v-model="userpwd"></div>
        <button @click="add">提交</button>


        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
                <div class="item">操作</div>
            </div>

            <div class="tr" v-for="item in list">
                <div class="item">{{item.id}}</div>
                <div class="item">
                    <span v-if="!item.checked">{{item.username}}</span>
                    <span v-else><input v-model="item.username" /></span>
                </div>
                <div class="item">
                    <span v-if="!item.checked">{{item.userpwd}}</span>
                    <span v-else><input v-model="item.userpwd" /></span>
                </div>
                <div class="item" @click="del(item.id)">删除</div>
                <div class="item">
                    <span v-if="!item.checked" @click="upd(item)">修改</span>
                    <span v-else>
                        <button @click="save">提交</button>
                        <button @click="cancel">取消</button>
                    </span>
                </div>
            </div>

            <!-- <div class="big" v-show="flag">
                <div>用户名:<input v-model="updname" /></div>
                <div>密&nbsp;&nbsp;&nbsp;码:<input v-model="updpwd" /></div>
                <div>
                    <button @click="save">提交</button>
                    <button @click="cancel">取消</button>
                </div>
            </div> -->

        </div>


    </div>

</body>
<script>

    new Vue({
        el: "#app",

        data: {
            username: "",
            userpwd: "",

            updname: "",
            updpwd: "",

            updid: "",


            list: [],
        },

        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2345/userinfos",
                params: {},
            })
            this.list = list.map(item => {
                this.checked = false;
                return item;
            });
        },

        methods: {
            async del(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2345/userinfos/" + id,
                })
                this.list = this.list.filter(item => item.id != id);
            },

            async add() {
                // console.log(this.username, this.userpwd);
                // console.log(this.list.some(item => item.username == this.username));
                if (this.list.some(item => item.username == this.username)) {
                    alert("该用户名已存在")
                } else {
                    let { data } = await axios({
                        method: "post",
                        url: "http://localhost:2345/userinfos",
                        data: {
                            username: this.username,
                            userpwd: this.userpwd,
                        }
                    })
                    this.list.push(data);
                }
            },

            upd(info) {
                info.checked = true;
                info.updname = info.username;
                info.updpwd = info.userpwd;
                // info.updid = info.id;

            },
            async save(item) {
                let username = this.updname;
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfos",
                    params: {
                        username: item.username
                    }
                });

                if (list.length) {

                    let info = list[0];

                    if (info.id != item.id) {
                        alert("该用户名已经存在");
                        return false;
                    }
                }

                await axios({
                    method: "patch",
                    url: "http://localhost:2345/userinfos/" + item.id,
                    data: {
                        username: item.username,
                        userpwd: item.userpwd
                    }
                });

                item.checked = false;
            },


            cancel() {
                item.checked = false;
                item.username = item.updname;
                item.userpwd = item.updpwd;
            }





        }

    })



</script>

</html>